package iabincatalog.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Class for programatically download a webpage - can be used to get the XML from
 * GBIF/IABIN portals.
 * 
 * To use this, configure the direct-access.properties file.
 * 
 * file: file where the webpage code will be stored
 * url:  url address whose webpage will be downloaded
 * 
 * @author Nelson
 *
 */
public class DirectAccess 
{
	// Logger
	private static Logger logger = Logger.getLogger(DirectAccess.class);
	
	/**
	 * Main method that calls the downloadPageToFile() method.
	 */
	public static void main(String[] args) throws Exception
	{
		Properties properties = new Properties();
		properties.load(ClassLoader.getSystemResourceAsStream("direct-access.properties"));
		
		downloadPageToFile(properties.getProperty("url"), properties.getProperty("file"));
	}
	
	/**
	 * Downloads a page using one of the algorithms developed (ideally the best).
	 * 
	 * @param theUrlString The String representation of the URL
	 * @param theFileName Target file
	 */
	public static void downloadPageToFile(String theUrlString, String theFileName) throws Exception
	{
		downloadPageToFileOne(theUrlString, theFileName);
	}
	
	/**
	 * Downloads a page using: BufferedReader.readLine() [buffer: 8192 - default]
	 */
	protected static void downloadPageToFileOne(String theUrlString, String theFileName) throws Exception
	{
		logger.debug("Downloading webpage: " + theUrlString);
		
		URL url = new URL(theUrlString);
		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
		
		File file = new File(theFileName);
		BufferedWriter out = new BufferedWriter(new FileWriter(file));
		
		String line;
		while ((line = in.readLine()) != null)
		{
			out.write(line);
			out.newLine();
		}
		
		in.close();
		out.close();
		
		logger.debug("Webpage saved to file: " + theFileName);
	}
	
	/**
	 * Downloads a page using: BufferedReader.readLine() [buffer: 65536]	
	 */
	protected static void downloadPageToFileTwo(String theUrlString, String theFileName) throws Exception
	{
		logger.debug("Downloading webpage: " + theUrlString);
		
		URL url = new URL(theUrlString);
		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()), 65536);
		
		File file = new File(theFileName);
		BufferedWriter out = new BufferedWriter(new FileWriter(file));
		
		String line;
		while ((line = in.readLine()) != null)
		{
			out.write(line);
			out.newLine();
		}
		
		in.close();
		out.close();
		
		logger.debug("Webpage saved to file: " + theFileName);
	}
	
	/**
	 * Downloads a page using: BufferedReader.readLine() [buffer: 64]
	 */
	protected static void downloadPageToFileThree(String theUrlString, String theFileName) throws Exception
	{
		logger.debug("Downloading webpage: " + theUrlString);
		
		URL url = new URL(theUrlString);
		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()), 64);
		
		File file = new File(theFileName);
		BufferedWriter out = new BufferedWriter(new FileWriter(file));
		
		String line;
		while ((line = in.readLine()) != null)
		{
			out.write(line);
			out.newLine();
		}
		
		in.close();
		out.close();
		
		logger.debug("Webpage saved to file: " + theFileName);
	}
	
	/**
	 * Downloads a page using: BufferedReader.read()
	 */
	protected static void downloadPageToFileFour(String theUrlString, String theFileName) throws Exception
	{
		logger.debug("Downloading webpage: " + theUrlString);
		
		URL url = new URL(theUrlString);
		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()), 64);
		
		File file = new File(theFileName);
		BufferedWriter out = new BufferedWriter(new FileWriter(file));
		
		int ch = in.read();
		while (ch != -1)
		{
			out.write(ch);
			ch = in.read();
		}
		
		in.close();
		out.close();
		
		logger.debug("Webpage saved to file: " + theFileName);
	}
	
	/**
	 * Downloads a page using: BufferedReader.readLine() [buffer: 1]
	 */
	protected static void downloadPageToFileFive(String theUrlString, String theFileName) throws Exception
	{
		logger.debug("Downloading webpage: " + theUrlString);
		
		URL url = new URL(theUrlString);
		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()), 1);
		
		File file = new File(theFileName);
		BufferedWriter out = new BufferedWriter(new FileWriter(file));
		
		String line;
		while ((line = in.readLine()) != null)
		{
			out.write(line);
			out.newLine();
		}
		
		in.close();
		out.close();
		
		logger.debug("Webpage saved to file: " + theFileName);
	}
	
}
